package com.java110.mall.cart.api;

import com.alibaba.fastjson.JSONObject;
import com.java110.mall.cart.bmo.goldBusiness.IDeleteGoldBusinessBMO;
import com.java110.mall.cart.bmo.goldBusiness.IGetGoldBusinessBMO;
import com.java110.mall.cart.bmo.goldBusiness.ISaveGoldBusinessBMO;
import com.java110.mall.cart.bmo.goldBusiness.IUpdateGoldBusinessBMO;
import com.java110.dto.gold.GoldBusinessDto;
import com.java110.po.gold.GoldBusinessPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/goldBusiness")
public class GoldBusinessApi {

    @Autowired
    private ISaveGoldBusinessBMO saveGoldBusinessBMOImpl;
    @Autowired
    private IUpdateGoldBusinessBMO updateGoldBusinessBMOImpl;
    @Autowired
    private IDeleteGoldBusinessBMO deleteGoldBusinessBMOImpl;

    @Autowired
    private IGetGoldBusinessBMO getGoldBusinessBMOImpl;

    /**
     * 微信保存消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /goldBusiness/saveGoldBusiness
     * @path /app/goldBusiness/saveGoldBusiness
     */
    @RequestMapping(value = "/saveGoldBusiness", method = RequestMethod.POST)
    public ResponseEntity<String> saveGoldBusiness(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "storeId", "请求报文中未包含storeId");
        Assert.hasKeyAndValue(reqJson, "amount", "请求报文中未包含amount");


        GoldBusinessPo goldBusinessPo = BeanConvertUtil.covertBean(reqJson, GoldBusinessPo.class);
        return saveGoldBusinessBMOImpl.save(goldBusinessPo);
    }

    /**
     * 微信修改消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /goldBusiness/updateGoldBusiness
     * @path /app/goldBusiness/updateGoldBusiness
     */
    @RequestMapping(value = "/updateGoldBusiness", method = RequestMethod.POST)
    public ResponseEntity<String> updateGoldBusiness(@RequestBody JSONObject reqJson) {

        Assert.hasKeyAndValue(reqJson, "storeId", "请求报文中未包含storeId");
        Assert.hasKeyAndValue(reqJson, "amount", "请求报文中未包含amount");
        Assert.hasKeyAndValue(reqJson, "busiId", "busiId不能为空");


        GoldBusinessPo goldBusinessPo = BeanConvertUtil.covertBean(reqJson, GoldBusinessPo.class);
        return updateGoldBusinessBMOImpl.update(goldBusinessPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param reqJson
     * @return
     * @serviceCode /goldBusiness/deleteGoldBusiness
     * @path /app/goldBusiness/deleteGoldBusiness
     */
    @RequestMapping(value = "/deleteGoldBusiness", method = RequestMethod.POST)
    public ResponseEntity<String> deleteGoldBusiness(@RequestBody JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "communityId", "小区ID不能为空");

        Assert.hasKeyAndValue(reqJson, "busiId", "busiId不能为空");


        GoldBusinessPo goldBusinessPo = BeanConvertUtil.covertBean(reqJson, GoldBusinessPo.class);
        return deleteGoldBusinessBMOImpl.delete(goldBusinessPo);
    }

    /**
     * 微信删除消息模板
     *
     * @param storeId 小区ID
     * @return
     * @serviceCode /goldBusiness/queryGoldBusiness
     * @path /app/goldBusiness/queryGoldBusiness
     */
    @RequestMapping(value = "/queryGoldBusiness", method = RequestMethod.GET)
    public ResponseEntity<String> queryGoldBusiness(@RequestHeader(value = "store-id", required = false) String storeId,
                                                    @RequestParam(value = "amount",required = false) String amount,
                                                    @RequestParam(value = "busiId",required = false) String busiId,
                                                    @RequestParam(value = "buyerName",required = false) String buyerName,
                                                    @RequestParam(value = "page") int page,
                                                    @RequestParam(value = "row") int row) {
        GoldBusinessDto goldBusinessDto = new GoldBusinessDto();
        goldBusinessDto.setPage(page);
        goldBusinessDto.setRow(row);
        goldBusinessDto.setStoreId(storeId);
        goldBusinessDto.setAmount(amount);
        goldBusinessDto.setBusiId(busiId);
        goldBusinessDto.setBuyerName(buyerName);
        return getGoldBusinessBMOImpl.get(goldBusinessDto);
    }
}
